package com.gwl.autoqq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReceiveMessage {
    private static ObjectMapper objectMapper = new ObjectMapper();


    public static String svTime(String timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

        // 将时间戳转换为 Date 对象
        Date date = new Date(Long.parseLong(timestamp));

        // 使用 SimpleDateFormat 格式化 Date 对象
        return sdf.format(date);
    }

    public static void main(String[] args) {
        ServerSocket listenSocket = null;
        try {
            listenSocket = new ServerSocket();
            SocketAddress socketAddress = new InetSocketAddress("jzy.gwl.asia", 5701);
            listenSocket.bind(socketAddress);
            while (true){
                Socket clientSocket = listenSocket.accept();
//                System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());

                InputStream inputStream = clientSocket.getInputStream();
                OutputStream outputStream = clientSocket.getOutputStream();

                byte[] buffer = new byte[1024];
                int length = inputStream.read(buffer);
                String request = new String(buffer, 0, length, "UTF-8");
                // 过滤请求
                Pattern pattern = Pattern.compile("\\{.*\\}");
                Matcher matcher = pattern.matcher(request);
                JSONObject response = null;
                if (matcher.find()) {
                    String json = matcher.group(0);
                    response = JSON.parseObject(json);
                }

                // 收到信息事件
                if(response.getString("post_type").equals("message")){
                    String message_type = response.getString("message_type");
                    String time = response.getString("time");
                    String message_id = response.getString("message_id");
                    String message = response.getString("message");
                    String sender_user_id = response.getJSONObject("sender").getString("user_id");
                    String sender_nickname = response.getJSONObject("sender").getString("nickname");
                    System.out.println("信息类型是: "+ message_type);
                    System.out.println( "收到的信息是: " + message);
                    System.out.println( "发送的用户id是: " + sender_user_id);
                    System.out.println( "发送给的用户id是: " + sender_nickname);
                    System.out.println( "时间是: " + svTime(time));
                    System.out.println( "消息id是: " + message_id);
                    System.out.println( "========================================");
                    System.out.println();
                    System.out.println();
                }


//                System.out.println("Request: " + request);
                clientSocket.close();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
